<template>
  <div class="activity">
    <table-title
        title="疾病类型管理"
        button-text="新增疾病类型"
        button-icon="el-icon-circle-plus-outline"
        @click="addDetail">
    </table-title>

    <el-table
        border
        stripe
        :data="tableData"
        v-loading="loading">
      <el-table-column align="center" type="index" label="序号" width="128"></el-table-column>
      <el-table-column align="center" prop="treatmentType" label="名称"></el-table-column>

      <el-table-column align="center" label="操作" width="256">
        <template slot-scope="scope">
          <el-button
              type="primary"
              slot="reference"
              icon="el-icon-edit"
              size="mini"
              @click="editDetail(scope.row.typeId, scope.row.treatmentType)">
          </el-button>

          <el-button
              type="danger"
              slot="reference"
              icon="el-icon-delete"
              size="mini"
              @click="deleteDetail(scope.row.typeId)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        background
        layout="prev, pager, next"
        :page-count="totalCount"
        :current-page="currentPage"
        @current-change="changePage"
        :hide-on-single-page="true"
        class="page">
    </el-pagination>
  </div>
</template>
<script>
import TableTitle from "../../../components/TableTitle";

export default {
  name: "TreatmentType",
  components: {TableTitle},
  data() {
    return {
      // 数据
      tableData: [
        /*{
          "treatmentType": "string",
          "typeId": 0
        }*/
      ],

      // 设置
      loading: true,
      currentPage: 1,
      totalCount: 1
    };
  },
  methods: {
    /**
     * 发布
     */
    addDetail() {
      this.openEditor().then((value => {
        this.axios.post(this.$store.state.api + '/treatment-type/add', {
          treatmentType: value,
        }).then((response) => {
          this.$message({
            type: response.data.data ? 'success' : 'error',
            message: response.data.data ? '添加成功！' : '添加失败！'
          });
          // 获取数据
          this.changePage(1);
        }).catch((error) => {
          console.log(error);
        });
      }));
    },

    /**
     * 编辑
     */
    editDetail(id, treatmentType) {
      this.openEditor(false, treatmentType).then((value => {
        this.axios.put(this.$store.state.api + '/treatment-type/update', [{
          treatmentType: value,
          typeId: id
        }]).then((response) => {
          this.$message({
            type: response.data.data ? 'success' : 'error',
            message: response.data.data ? '修改成功！' : '修改失败！'
          });
          // 获取数据
          this.changePage(1);
        }).catch((error) => {
          console.log(error);
        });
      }));
    },

    // 打开
    openEditor(add = true, inputValue = '') {
      return new Promise((resolve => {
        this.$prompt('', add ? '新增疾病类型' : '修改疾病类型', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: inputValue,
          closeOnClickModal: false,
          inputValidator: (value) => {
            if (!value) return false;
            return true;
          },
          inputErrorMessage: '疾病类型不能为空'
        }).then(({value}) => {
          resolve(value);
        });
      }))
    },


    /**
     * 删除
     */
    deleteDetail(id) {
      this.$confirm("将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.axios.delete(this.$store.state.api + '/treatment-type/del?ids=' + id).then((response) => {
          if (response.data.code == 200) {
            this.$message({
              type: response.data.data ? 'success' : 'error',
              message: response.data.data ? '删除成功！' : '删除失败！'
            });
          }
          // 获取数据
          this.changePage(1);
        }).catch((error) => {
          console.log(error);
        });
      });
    },

    /**
     * 获取数据
     *
     * @param val
     */
    changePage(val) {
      this.currentPage = val;
      const url = this.$store.state.api + '/treatment-type/list?pageNum=' + this.currentPage + '&pageSize=15';
      this.axios.post(url, {})
          .then((response) => {
            if (response.data.code == 200) {
              this.tableData = response.data.data.records;
              this.totalCount = response.data.data.pages;
              this.loading = false;
            }
          })
          .catch((error) => {
            console.log(error);
          });
    }
  },
  created() {
    /**
     * 获取数据
     */
    this.changePage(this.currentPage);
  }
};
</script>

<style lang="less" scoped>
@import "../../../assets/less/base.less";

.activity {
  color: @fontColor;
  min-height: 100%;
  background: white;
}
</style>
